﻿<!DOCTYPE html>
<html>
<head>
	<script src="//code.jquery.com/jquery-3.6.4.js" type="text/javascript"></script>
	<script src="../../jsrender.js" type="text/javascript"></script>
	<link href="../resources/demos.css" rel="stylesheet" type="text/css" />

	<link href="../resources/movielist.css" rel="stylesheet" type="text/css" />
</head>
<body>
<a href="../demos.html">JsRender Demos</a><br />

<h3>Associating specific contextual helpers with templates</h3>

<div class="subhead">Including helpers in a template definition.</div>
<pre>
$.templates({
    appTmpl: {
        markup:"#appTmpl",
        helpers: {
            supplierUtils: ...
        }
    }
});
</pre>

<div class="subhead">Passing different helpers to a sub-template based on the context where it is used.</div>
<pre>
{{for suppliers tmpl="personTmpl" ~utils=~supplierUtils/}}
</pre>

<div class="subhead">Accessing helper from nested template:</div>
<pre>
&lt;b>ID:&lt;/b> &lt;em>{{:~utils.format(id)}}&lt;/em>
</pre>

<!--================ Demo ================-->

<div id="target"></div>

<script id="appTmpl" type="text/x-jsrender">
	<h3>Suppliers</h3>
	<table><tbody>
		{{for suppliers tmpl="personTmpl" ~utils=~supplierUtils/}}
	</tbody></table>

	<h3>Customers</h3>
	<table><tbody>
		{{for customers tmpl="personTmpl" ~utils=~customerUtils/}}
	</tbody></table>
</script>

<script id="personTmpl" type="text/x-jsrender">
	<tr>
		<td><b>ID:</b> <em>{{:~utils.format(id)}}</em></td>
		<td>{{:firstName}} {{:lastName}}</td>
	</tr>
</script>​

<script type='text/javascript'>//<![CDATA[
	var people = [
		{
			id: "abc22",
			firstName: "Jeff",
			lastName: "Johnson"
		},
		{
			id: "pq44",
			firstName: "Rachel",
			lastName: "Roberts"
		},
		{
			id: "xyz99",
			firstName: "Jacques",
			lastName: "Bertin"
		}
	];

	var model = {
		suppliers: [people[0], people[2]],
		customers: [people[1], people[2]]
	}

	$.templates({
		personTmpl: "#personTmpl",
		appTmpl: {
			markup:"#appTmpl",
			helpers: {
				supplierUtils: {
					format:function(val){return "Supplier:" + val.toUpperCase();}
				},
				customerUtils: {
					format:function(val){return "Customer:" + val.toLowerCase();}
				}
			}
		}
	});

	$("#target").html( $.render.appTmpl( model ));
</script>

</body>
</html>

